package com.suning.sawp.service.impl.storeman;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.suning.sawp.constants.ErrorCodeConstants;
import com.suning.sawp.dto.common.ReturnMsg;
import com.suning.sawp.dto.storeman.FriendInfoBean;
import com.suning.sawp.dto.storeman.QueryFriendListResultBean;
import com.suning.sawp.dto.storeman.RetUserInfo;
import com.suning.sawp.exception.MobileException;
import com.suning.sawp.intf.storeman.UserShopGuideService;
import com.suning.sawp.service.util.GsonUtils;
import com.suning.vgs.follow.remote.intf.RmsRemoteService;
import com.suning.vgs.follow.remote.intf.StoreManRemoteService;
import com.suning.vgs.follow.remote.member.SnMemberInfo;
import com.suning.vgs.follow.remote.store.MemberContact;

@Service("userShopGuideService")
public class UserShopGuideServiceImpl implements UserShopGuideService {
    
    private static final Logger LOGGER = 
            LoggerFactory.getLogger(UserShopGuideServiceImpl.class);
    
    @Resource
    RmsRemoteService rmsRemoteService;
    
    @Resource
    StoreManRemoteService storeManRemoteService;
    
    @Override
    public String getUserProfile(String userId) {
        ReturnMsg<RetUserInfo> msg = new ReturnMsg<RetUserInfo>();
        try {
            List<String> custNos = new ArrayList<String>();
            custNos.add(userId);
            List<SnMemberInfo> list = rmsRemoteService.getMultiUsers(custNos);
            if (CollectionUtils.isEmpty(list)) {
                msg.setError(ErrorCodeConstants.ERR_1009);
            } else {
                RetUserInfo retUserInfo = convertRetUserInfo(list.get(0));
                msg.setData(retUserInfo);
            }
        } catch (Exception e) {
            LOGGER.error("调用RMS查询云导购用户出错, 参数:{}", userId, e);
            msg.setError(ErrorCodeConstants.ERR_1009);
        }
        
        return GsonUtils.toJson(msg, false);
    }
    
    /**
     * 
     * 功能描述: 组装客户端所需字段<br>
     * 〈功能详细描述〉
     *
     * @param userBean
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    private RetUserInfo convertRetUserInfo(SnMemberInfo userBean) {
        RetUserInfo retUserInfo = new RetUserInfo();
        retUserInfo.setUserId(getValue(userBean.getUserId()));
        retUserInfo.setGender(getValue(userBean.getGender()));
        retUserInfo.setHeadPicNo(getValue(userBean.getHeadPicNo()));
        retUserInfo.setHeadPicType(getValue(userBean.getHeadPicType()));
        retUserInfo.setMobile(getValue(userBean.getMobile()));
        retUserInfo.setNickname(getValue(userBean.getNickname()));
        retUserInfo.setRemarkName("");
        retUserInfo.setResume("");
        retUserInfo.setSignature("");
        retUserInfo.setUserType("0");
        retUserInfo.setSource("");
        retUserInfo.setEmail(getValue(userBean.getEmail()));
        
        return retUserInfo;
    }

    private String getValue(String value) {
        return StringUtils.isNotBlank(value) ? value : "";
    }

    @Override
    public String getMultiUserProfiles(List<String> userIds) {
        ReturnMsg<List<RetUserInfo>> msg = new ReturnMsg<List<RetUserInfo>>();
        try {
            List<SnMemberInfo> list = rmsRemoteService.getMultiUsers(userIds);
            if (CollectionUtils.isEmpty(list)) {
                msg.setError(ErrorCodeConstants.ERR_1009);
            } else {
                List<RetUserInfo> datas = new ArrayList<RetUserInfo>();
                for (SnMemberInfo bean : list) {
                    datas.add(convertRetUserInfo(bean));
                }
                msg.setData(datas);
            }
        } catch (Exception e) {
            LOGGER.error("调用UMS批量查询云导购用户出错.", e);
            msg.setError(ErrorCodeConstants.ERR_1009);
        }
        
        return GsonUtils.toJson(msg, false);
    }

    @Override
    public QueryFriendListResultBean queryFriendList(String userId, String queriedFriendIds) throws MobileException {
     // 创建结果bean
        QueryFriendListResultBean result = new QueryFriendListResultBean();

        List<String> friendCustNos = null;
        if (StringUtils.isNotBlank(queriedFriendIds)) {
            String[] friendIds = queriedFriendIds.split(",");
            friendCustNos = Arrays.asList(friendIds);
        }
        List<MemberContact> contacts = 
                storeManRemoteService.getMemberContactsByCgId(userId, friendCustNos);
        if (CollectionUtils.isNotEmpty(contacts)) {
            List<FriendInfoBean> friendList = new ArrayList<FriendInfoBean>();
            for (MemberContact contact : contacts) {
                friendList.add(new FriendInfoBean(contact));
            }
            result.setFriendList(friendList);
        }
        
        return result;
    }
    
}
